<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>面向对象-选项卡</title>
  <style>
    .container {
      width: 600px;
      height: 400px;
      border: 10px solid #333;
      margin: 30px auto;
      display: flex;
      flex-direction: column;
    }

    .header {
      height: 40px;
      display: flex;
      line-height: 40px;
    }

    .header div {
      flex: 1;
      background-color: hotpink;
      text-align: center;
      font-size: 36px;
      color: white;
    }

    .header .active {
      background-color: orange;
    }

    .content {
      display: none;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="header">
      <div class="active">热卖</div>
      <div>精品</div>
      <div>推荐</div>
    </div>
    <div class="body">
      <div class="content" style="display: block;">1</div>
      <div class="content">2</div>
      <div class="content">3</div>
    </div>
  </div>
  <script>
    function Tab(headers, contents) {
      this.headers = headers;
      this.contents = contents;
      // 这里的this指向t
      this.init()
    }
    Tab.prototype.init = function () {
      this.headers.forEach((header, index) => {
        // 清除样式
        //保存外层this 里面的this指向header
        that = this;
        header.onclick = function () {
          that.headers.forEach(item => item.className = '');
          this.className = 'active';
          that.contents.forEach(content => {
            content.style.display = 'none'
          })
          that.contents[index].style.display = 'block'
        }
      })
    }
    var headers = document.querySelectorAll('.header>div');
    var contents = document.querySelectorAll('.body>.content');
    var t = new Tab(headers, contents)
  </script>
</body>

</html>